/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package io.renren.modules.sys.controller;

import io.renren.common.annotation.SysLog;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.sys.entity.SysDictEntity;
import io.renren.modules.sys.service.SysEmployeeService;
import io.renren.modules.sys.service.SysDictService;
import org.apache.commons.lang.ArrayUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 系统字典
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/sys/dict")
public class SysDictController extends AbstractController {
	@Autowired
	private SysDictService sysDictService;

	/**
	 * 所有字典列表
	 */
	@GetMapping("/list")
	@RequiresPermissions("sys:dict:list")
	public R list(@RequestParam Map<String, Object> params){
		PageUtils page = sysDictService.queryPage(params);
		return R.ok().put("page", page);
	}

	/**
	 * 字典信息
	 */
	@GetMapping("/info/{id}")
	@RequiresPermissions("sys:dict:info")
	public R info(@PathVariable("id") Long id){
		SysDictEntity dict = sysDictService.getById(id);
		return R.ok().put("dict", dict);
	}
	
	/**
	 * 保存字典
	 */
	@SysLog("保存字典")
	@PostMapping("/save")
	@RequiresPermissions("sys:dict:save")
	public R save(@RequestBody SysDictEntity dict){
		ValidatorUtils.validateEntity(dict, AddGroup.class);
		sysDictService.saveDict(dict);
		
		return R.ok();
	}
	
	/**
	 * 修改字典
	 */
	@SysLog("修改字典")
	@PostMapping("/update")
	@RequiresPermissions("sys:dict:update")
	public R update(@RequestBody SysDictEntity org){
		ValidatorUtils.validateEntity(org, UpdateGroup.class);
		sysDictService.update(org);
		
		return R.ok();
	}
	
	/**
	 * 删除字典
	 */
	@SysLog("删除字典")
	@PostMapping("/delete")
	@RequiresPermissions("sys:dict:delete")
	public R delete(@RequestBody Long[] dictIds){
		sysDictService.deleteBatch(dictIds);
		
		return R.ok();
	}
}
